package seatiger.tomcat.catalina.startup;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSetBase;
import seatiger.tomcat.catalina.Container;
import seatiger.tomcat.catalina.core.StandardContext;

/**
 * 解析Context
 */
public class ContextRuleSet extends RuleSetBase {

    protected String prefix = null;

    public ContextRuleSet(String prefix){
        this.prefix = prefix;
    }
    @Override
    public void addRuleInstances(Digester digester) {
        digester.addObjectCreate(prefix + "Context", StandardContext.class);
        digester.addSetProperties(prefix + "Context");
        digester.addRule(prefix + "Context",new LifecycleListenerRule(digester,ContextConfig.class));
        digester.addSetNext(prefix + "Context",
                "addChild",Container.class.getName());

        //在Context增加阀
        digester.addObjectCreate(prefix+"Context/Valve",null,"className");
        digester.addSetProperties(prefix + "Context/Valve");
        digester.addSetNext(prefix + "Context/Valve",
                "addValve",
                "seatiger.tomcat.catalina.Valve");

        //在Context增加session管理器Manager
        digester.addObjectCreate(prefix+"Context/Manager",
                "seatiger.tomcat.catalina.session.StandardManager",
                "className");
        digester.addSetProperties(prefix + "Context/Manager");
        digester.addSetNext(prefix + "Context/Manager",
                "setManager",
                "seatiger.tomcat.catalina.Manager");
        //为session管理器增加持久化存储
        digester.addObjectCreate(prefix+"Context/Manager/Store",
                null,
                "className");
        digester.addSetProperties(prefix + "Context/Manager/Store");
        digester.addSetNext(prefix + "Context/Manager/Store",
                "setStore",
                "seatiger.tomcat.catalina.Store");
    }
}
